<template>
  <div class="supplier" id="grxx">

    <el-form :model="supplierForm" :rules="rules" ref="supplierForm" label-width="100px " class="demo-ruleForm">
      <h1>社区团购供应商招募</h1>
      <p>欢迎成为我们的供应商合作伙伴，共同发展，共同创造价值！</p>

      <!-- 图片上传 -->
      <el-form-item label="公司名称" prop="companyName" style="width: 60%;">
        <el-input v-model="supplierForm.companyName"></el-input>
      </el-form-item>
      <el-form-item label="法定代表人" prop="name" style="width: 60%;">
        <el-input v-model="supplierForm.name"></el-input>
      </el-form-item>

      <el-form-item label="联系方式" prop="phone" style="width: 60%;">
        <el-input v-model="supplierForm.phone"></el-input>
      </el-form-item>

      <el-form-item label="公司地址" prop="address" style="width: 60%;">
        <el-input type="textarea" v-model="supplierForm.address"></el-input>
      </el-form-item>

      <el-form-item label="详细地址" prop="detailAddress" style="width: 60%;">
        <el-input type="textarea" v-model="supplierForm.detailAddress"></el-input>
      </el-form-item>

      <el-form-item label="营业执照"  prop="img" style="width: 50%;">
        <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="supplierFormadd">提交</el-button>
        <el-button @click="resetForm('supplierForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//省市区
import { pcaTextArr } from "element-china-area-data";
export default {
  name: 'SupplierRecruitment',
  data() {
    return {
      pcaTextArr, //地址数据
      selectedOptions: [], //地址绑定,必须是数组
      ary: '',
      supplierForm: {},
      rules: {
        img: [
          {required: true, message: '请上传执照', trigger: 'blur'}
        ],
        companyName: [
          {required: true, message: '请输入公司名称', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入法定代表人', trigger: 'blur'}
        ],
        // phone: [
        //   {required: true, message: '请输入联系方式', trigger: 'blur'},
        //   {pattern: /^1[3|4|5|7|8][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        // ],
        detailAddress: [
          {required: true, message: '请输入详细地址', trigger: 'blur'}
        ],
        // productCategory: [
        //   {required: true, message: '请选择经营类型', trigger: 'change'}
        // ],
        //选择框
        selector1: [
          {required: true, message: '请选择公司地址', trigger: 'change'},
        ]
      }
    };
  },
  methods: {
    async supplierFormadd() {
      //拿用户id
      this.sysUsersId=this.$store.state.user.sysUsersId
      console.log(this.sysUsersId)
      //定义用户id
      let sysUsersId=this.sysUsersId
      // this.resetForm.sysUsersId=sysUsersId
      console.log(this.supplierForm)
      const {data: res} = await this.$http.post(`/supplierInfo/supplierInfoAdd/${sysUsersId}`,this.supplierForm)
      //消息提示
      this.$message({
        showClose: true,
        message: '信息填写成功,请等待审核',
        type: 'success'
      });
    },
    //图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      const selectors = ['selector1', 'selector2', 'selector3'];
      for (const selector of selectors) {
        this.$refs[selector].resetField();
      }
    },
    //级联地区改变
    handleChangeAddress(value) {
      console.log(value.join('-'));
    },
  },
  created() {
    let temp = this.ary.split('-')
    console.log('ary', temp);
    this.selectedOptions = temp
  }

}
</script>

<style>
.supplier {
  width: 1100px;
  margin: 0 auto;
  padding: 20px;
  /*border: 1px solid #ddd;*/
  border-radius: 5px;
}


/* 图片上传 */
.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: black;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
